package com.ideaaedi.springcloud.jd.user.controller.sys;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.ideaaedi.springcloud.jd.commonds.entity.PageInfo;
import com.ideaaedi.springcloud.jd.commonds.entity.Result;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysRoleAddReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysRoleBindApiReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysRoleBindMenuReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysRoleListReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysRoleQueryApiReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysRoleQueryMenuReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysRoleUpdateReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.SysApiResourceListRespVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.SysQueryMenuRespVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.SysRoleDetailRespVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.SysRoleListRespVO;
import com.ideaaedi.springcloud.jd.user.service.SysApiResourceService;
import com.ideaaedi.springcloud.jd.user.service.SysRoleMenuReService;
import com.ideaaedi.springcloud.jd.user.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 系统 - 角色
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 2021.0.1.A
 */
@RestController
@ApiSort(value = 7)
@RequestMapping("/sys-role")
@Api(value = "系统-角色", tags = {"系统-角色"})
public class SysRoleController {
    
    @Resource
    private SysRoleService sysRoleService;
    
    @Resource
    private SysApiResourceService sysApiResourceService;
    
    @Resource
    private SysRoleMenuReService sysRoleMenuReService;
    
    /**
     * 新增
     *
     * @param req
     *         参数
     *
     * @return 修改后的角色详情
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增")
    @ApiOperationSupport(order = 1)
    public Result<SysRoleDetailRespVO> add(@Validated @RequestBody SysRoleAddReqVO req) {
        return Result.success(sysRoleService.add(req));
    }
    
    /**
     * 删除
     *
     * @param roleId 角色id
     *
     * @return 删除了的角色id
     */
    @ApiOperation(value = "删除")
    @ApiOperationSupport(order = 2)
    @DeleteMapping("/delete/{roleId}")
    public Result<SysRoleDetailRespVO> delete(@ApiParam(value = "角色id", required = true)
                                              @NotNull(message = "roleId不能为空") @PathVariable("roleId") Long roleId) {
        return Result.success(sysRoleService.delete(roleId));
    }
    
    /**
     * 更新
     *
     * @param req
     *         参数
     *
     * @return 新增后的角色详情
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新")
    @ApiOperationSupport(order = 3)
    public Result<SysRoleDetailRespVO> update(@Validated @RequestBody SysRoleUpdateReqVO req) {
        return Result.success(sysRoleService.update(req));
    }
    
    /**
     * 列表
     *
     * @param req 参数
     *
     * @return 角色列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "列表")
    @ApiOperationSupport(order = 4)
    public Result<PageInfo<SysRoleListRespVO>> list(@Validated @RequestBody SysRoleListReqVO req) {
        return Result.success(sysRoleService.list(req));
    }
    
    /**
     * 详情
     *
     * @param roleId 角色id
     *
     * @return 角色详情
     */
    @ApiOperation(value = "详情")
    @ApiOperationSupport(order = 5)
    @GetMapping("/detail/{roleId}")
    public Result<SysRoleDetailRespVO> detail(@ApiParam(value = "角色id", required = true)
                                              @NotNull(message = "roleId不能为空") @PathVariable("roleId") Long roleId) {
        return Result.success(sysRoleService.detail(roleId));
    }
    
    /**
     * 给角色绑定菜单
     *
     * @param req 请求模型
     *
     * @return 角色拥有的菜单
     */
    @PostMapping("/bind-menu")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "给角色绑定菜单")
    public Result<List<SysQueryMenuRespVO>> bindMenu(@Validated @RequestBody SysRoleBindMenuReqVO req) {
        return Result.success(sysRoleMenuReService.bindMenu(req));
    }
    
    /**
     * 查询角色拥有的菜单
     *
     * @param req 请求参数
     *
     * @return 菜单信息集合
     */
    @ApiOperationSupport(order = 7)
    @GetMapping("/query-menu")
    @ApiOperation(value = "查询角色绑定的菜单")
    public Result<List<SysQueryMenuRespVO>> queryMenu(@Validated @RequestBody SysRoleQueryMenuReqVO req) {
        return Result.success(sysRoleMenuReService.queryMenu(req));
    }
    
    /**
     * 给角色绑定api
     *
     * @param req
     *         请求模型
     *
     * @return 操作是否成功
     */
    @PostMapping("/bind-api")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "给角色绑定api")
    public Result<List<SysApiResourceListRespVO>> bindApi(@Validated @RequestBody SysRoleBindApiReqVO req) {
        return Result.success(sysApiResourceService.bindApi(req));
    }
    
    /**
     * 查询角色拥有的api
     *
     * @param req 请求模型
     *
     * @return 操作是否成功
     */
    @PostMapping("/query-api")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "查询角色绑定的api")
    public Result<List<SysApiResourceListRespVO>> queryApi(@Validated @RequestBody SysRoleQueryApiReqVO req) {
        return Result.success(sysApiResourceService.queryApi(req.getRoleIds()));
    }
}
